<?php
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Features extends CI_Controller{
    
   /* function _construct()
    {
        parent::__construct();
        log_message('debug','Features controller initialized');
        
    }*/
    function Features()
    {
        parent::__construct();
        $this->load->model('features_model');
        $this->load->model('counter_model');
        $this->load->library('pagination');
    }
    function index()
    {
        $per_page = 10;
        $offset = 0 + $this->uri->segment(4);
        $config['base_url'] = base_url() . 'admin/features/index';
        $config['total_rows'] = $this->counter_model->feature_group_count('CAR');
        $config['uri_segment'] = '4';
        $config['full_tag_open'] = '';
        $config['full_tag_close'] = '';
        $this->pagination->initialize($config);
        $data['feature_group']=$this->features_model->get_features_groups('CAR','asc',$per_page,$offset);
        $this->load->view('admin/admin_features_group',$data);
    }
    function get_features($code)
    {
        $per_page = 10;
        $offset = 0 + $this->uri->segment(5);
        $config['base_url'] = base_url() . 'admin/features/get_features/'.$code;
        $config['total_rows'] = $this->counter_model->feature_count('CAR',$code);
        $config['uri_segment'] = '5';
        $config['full_tag_open'] = '';
        $config['full_tag_close'] = '';
        $this->pagination->initialize($config);
        $data['code']=$code;
        $data['feature_list']=$this->features_model->get_features('CAR',$code,'asc',$per_page,$offset);
        $this->load->view('admin/admin_features',$data);
    }
    function feature_group_insert()
    {
        if ($this->input->post('insert')) {
            $feature_group = $_POST['feature'];
            $this->form_validation->set_rules('feature', 'feature', 'required|xss_clean|is_unique[feature.feature]');
            $this->form_validation->set_rules('code', 'code', 'required|xss_clean');
            if ($this->form_validation->run()) {
                if ($feature_group != 'Enter Feature Group')
                {
                    $code=$this->input->post('code');
                    $this->features_model->features_group_insert('CAR', $feature_group,$code);
                }
                else
                    $this->session->set_flashdata('message', 'Type a Feature Group Name');
                redirect('admin/features');
            } else {
                $this->session->set_flashdata('message', 'Feature Group already exists');
                redirect('admin/features');
            }
        }
    }
    function feature_insert()
    {
        if ($this->input->post('insert')) {
            $feature_group = $_POST['feature'];
            $this->form_validation->set_rules('feature', 'feature', 'required|xss_clean|is_unique[feature.feature]');
            $this->form_validation->set_rules('code', 'code', 'required|xss_clean');
            $this->form_validation->set_rules('slno', 'slno', 'required|xss_clean');
            $code=$_POST['code'];
            $slno=$_POST['slno'];
            if ($this->form_validation->run()) {
                if ($feature_group != 'Enter Feature')
                {
                    $this->features_model->feature_insert('CAR', $feature_group,$code,$slno);
                }
                else
                    $this->session->set_flashdata('message', 'Type a Feature Name');
                redirect('admin/features/get_features/'.trim($code));
            } else {
                $this->session->set_flashdata('message', 'Feature already exists');
                redirect('admin/features/get_features/'.trim($code));
            }
        }
    }
    function feature_group_update()
    {
        if ($this->input->post('update_feature_group')) {
            $feature_group = $_POST['feature'];
            $this->form_validation->set_rules('feature', 'feature', 'required|xss_clean|is_unique[feature.feature]');
            $this->form_validation->set_rules('code', 'code', 'required|xss_clean');
            $code=$this->input->post('code');
            if ($this->form_validation->run()) {
                   $this->features_model->features_group_modify('CAR',$feature_group,$code);
                
            } else {
                $this->session->set_flashdata('message', 'Feature Group already exists');
                redirect('admin/features');
            }
        }
        redirect('admin/features');
    }
    function feature_update()
    {
        if ($this->input->post('update_feature')) {
            $feature_group = $_POST['feature'];
            $this->form_validation->set_rules('feature', 'feature', 'required|xss_clean|is_unique[feature.feature]');
            $this->form_validation->set_rules('feature_id', 'feature_id', 'required|xss_clean');
            $id=$this->input->post('feature_id');
            $code=$this->input->post('code');
            if ($this->form_validation->run()) {
                   $this->features_model->feature_modify('CAR',$feature_group,$id);
                
            } else {
                $this->session->set_flashdata('message', 'Feature already exists');
                redirect('admin/features/get_features/'.$code);
            }
        }
        redirect('admin/features/get_features/'.$code);
    }
    function feature_group_delete($code)
    {
        $this->features_model->features_group_delete('CAR',$code);
        redirect('admin/features');
    }
    function feature_delete($code,$id)
    {
        $this->features_model->feature_delete('CAR',$id);
        redirect('admin/features/get_features/'.$code);
    }
    
    }
?>
